package com.qianchang.microfinance.view.mine.loan;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.LoanProductBean;
import com.qianchang.microfinance.bean.SubmitLoanBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.view.base.BaseRecyclerAdapter;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ViewUtils;
import com.dm.library.widgets.element.DTextView;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MyLoanAdapter extends BaseRecyclerAdapter<LoanProductBean> implements View.OnClickListener {

    private static final String TAG = "MyLoanAdapter";


    public MyLoanAdapter(Context context) {
        super(context, BaseRecyclerAdapter.ONLY_FOOTER);
    }


    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_my_loan, parent, false);
        return new MyLoanViewHolder(view);
    }

    @Override
    protected void onBindDefaultViewHolder(RecyclerView.ViewHolder holder, LoanProductBean item, int position) {
        MyLoanViewHolder myLoanViewHolder = (MyLoanViewHolder) holder;

        myLoanViewHolder.tvPeriod.setTextContent(item.getDeadline());
        String createTime = item.getCreateTime();
        myLoanViewHolder.tvApplyDate.setTextContent(ViewUtils.formatDateToDay(createTime));
        String status = item.getStatus();
        myLoanViewHolder.tvStatus.setTextContent(item.getStatusDesc());
        double loanAmount = item.getLoanAmount();

        if (loanAmount == 0) {
            myLoanViewHolder.tvMoney.setTextContent(FormatUtil.numKbPointFormat(item.getApplyAmount()));
        } else {
            myLoanViewHolder.tvMoney.setTextContent(FormatUtil.numKbPointFormat(loanAmount));
        }

        if ("4".equals(status)) {
            myLoanViewHolder.llStatus.setVisibility(View.GONE);
            myLoanViewHolder.tvSureAgreement.setVisibility(View.VISIBLE);
        } else {
            myLoanViewHolder.llStatus.setVisibility(View.VISIBLE);
            myLoanViewHolder.tvSureAgreement.setVisibility(View.GONE);
        }
        myLoanViewHolder.tvSureAgreement.setOnClickListener(this);
        myLoanViewHolder.tvSureAgreement.setTag(position);
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        Intent intent = new Intent(mContext, ConfirmAgreementActivity.class);
        Bundle bundle = new Bundle();
        LoanProductBean loanProductBean = getItems().get(position);
        bundle.putString(ConstantAppInfo.PROJECT_ID, loanProductBean.getProjectId());
        bundle.putInt(ConstantAppInfo.PROJECT_TYPE, loanProductBean.getProductType());
        bundle.putString(SubmitLoanBean.STATUS, loanProductBean.getStatusDesc());
        bundle.putString(SubmitLoanBean.LOAN_AMOUNT, loanProductBean.getApplyAmount());
        bundle.putInt(ConstantAppInfo.FROM, 1);
        intent.putExtras(bundle);
        mContext.startActivity(intent, bundle);
    }


    static class MyLoanViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_money)
        DTextView tvMoney;
        @BindView(R.id.tv_period)
        DTextView tvPeriod;
        @BindView(R.id.tv_apply_title)
        DTextView tvApplyTitle;
        @BindView(R.id.tv_apply_date)
        DTextView tvApplyDate;
        @BindView(R.id.tv_status)
        DTextView tvStatus;
        @BindView(R.id.tv_sure_agreement)
        DTextView tvSureAgreement;
        @BindView(R.id.ll_status)
        LinearLayout llStatus;

        public MyLoanViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
